package com.lcy.app.activity.child;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lcy.app.R;
import com.lcy.app.api.domain.SysBusinessFileDTO;
import com.lcy.app.common.util.DownloadHelper;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.List;

public class ChildFileAdapter extends BaseAdapter {

    private Context context; //上下文
    private Integer layoutId; //布局子控件id
    private List<SysBusinessFileDTO> list; //数据源

    public ChildFileAdapter() {
    }

    //构造器
    public ChildFileAdapter(Context context, Integer layoutId, List<SysBusinessFileDTO> list) {
        this.context = context;
        this.layoutId = layoutId;
        this.list = list;
    }

    //继承BaseAdapter需重写的四个方法
    public int getCount() {
        return list.size();
    }//数据个数

    public Object getItem(int position) {
        return list.get(position);
    }//对应位置的item

    public long getItemId(int position) {
        return position;
    }//返回item下标位置

    //getView最重要的一个函数，position当前item的下标，convertView当前的视图，parent当前视图的父视图
    public View getView(int position, View convertView, ViewGroup parent) {
        SysBusinessFileDTO sysBusinessFileDTO = list.get(position);

        if ("video".equals(sysBusinessFileDTO.getPlayer())){
//            return getVideoView(convertView,sysBusinessFileDTO);
        }
        return getFileView(convertView, sysBusinessFileDTO);
    }

    private View getFileView(View convertView, SysBusinessFileDTO sysBusinessFileDTO) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.activity_child_file, null);
        }
        //获取列表项中的控件
        TextView filename = convertView.findViewById(R.id.filename);
        TextView size = convertView.findViewById(R.id.size);
        //给数据项填充数据

        filename.setText(sysBusinessFileDTO.getOriginal());
        size.setText(String.format("%s【%s】", sysBusinessFileDTO.getFormattedFileSize(), sysBusinessFileDTO.getType()));
        //返回列表项
        return convertView;
    }

    private View getVideoView( View convertView, SysBusinessFileDTO sysBusinessFileDTO) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.activity_child_video, null);
        }

        MediaPlayer mediaPlayer = new MediaPlayer();
        // 设置音频流类型（‌可选）AudioManager.STREAM_MUSIC
        AudioAttributes attributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
                .setContentType(AudioAttributes.CONTENT_TYPE_MOVIE)
                .build();

        mediaPlayer.setAudioAttributes(attributes);
        try {
            mediaPlayer.setDataSource(DownloadHelper.getMusicLink(null,sysBusinessFileDTO.getFileId()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        SurfaceView surfaceView = convertView.findViewById(R.id.myideo);
        // 设置视频显示到SurfaceView上
        surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                try {
                    mediaPlayer.setDataSource(DownloadHelper.getMusicLink(null,sysBusinessFileDTO.getFileId()));

                    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);//设置播放声音类型
                    mediaPlayer.prepare();
                    mediaPlayer.setDisplay(surfaceView.getHolder());

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

            }
        });
        // 异步准备视频数据源（‌这通常会在后台线程中进行）‌
        mediaPlayer.prepareAsync();

        // 设置准备完成监听器
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                // 播放视频（‌这里应该在准备完成后调用）‌
                mp.start();
                // 可以设置其他监听器，‌如OnCompletionListener和OnErrorListener，‌以处理播放结束和错误情况（‌略）‌
            }
        });

        return convertView;
    }
}
